package com.unicom.projt.fee;

import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-16
 * Time: 下午4:37
 */
public class PaymentQueryController extends AbstractHttpController<String> {
    private PaymentSuccessPage successPage;

    public PaymentQueryController(PaymentSuccessPage successPage) {
        super(String.class);
        this.successPage = successPage;
    }

    public void queryPayment(String phoneNumber) {
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_AVAILABLE_BALANCE).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }


    @Override
    public void onRequestFinish(String response) {
        successPage.hideProgressDialog();
        if (response == null || "".equals(response)) {
            ToastUtils.showMessage(successPage, "错误，请稍候再试");
            return;
        }
        String respCode = SimpleXmlParser.getResponseCode(response);
        if (ResponseCode.SUCCESS.equals(respCode)) {
            String svcCont = SimpleXmlParser.getElementsValue(response, "SvcCont").get(0);
            String record = SimpleXmlParser.getElementsValue(svcCont, "Record").get(0);
            String amount = SimpleXmlParser.getElementsValue(record, "Amount").get(0);
//            String amountValue = StringUtils.liToYuan(amount);
//            successPage.updateAmount(amount);
        } else {
            ToastUtils.showMessage(successPage, "查询出错," + SimpleXmlParser.getResponseDes(response));
        }
    }

    @Override
    public void onNetworkAbort() {
        successPage.hideProgressDialog();
        ToastUtils.showMessage(successPage, "错误，网络连接被中断.");
    }

    @Override
    public void onNetworkError() {
        successPage.hideProgressDialog();
        ToastUtils.showMessage(successPage, "错误，网络连接错误.");
    }

    @Override
    public void onResponseError() {
        successPage.hideProgressDialog();
        ToastUtils.showMessage(successPage, "错误，返回结果错误.");
    }
}
